package com.solming.leetcode.question;

//存在一个按升序排列的链表，给你这个链表的头节点 head ，请你删除所有重复的元素，使每个元素 只出现一次 。
//
//返回同样按升序排列的结果链表。
public class DeleteDuplicates0027 {

    public static ListNode deleteDuplicates(ListNode head) {

        ListNode slowNode = head;
        ListNode fastNode = head;

        while (fastNode != null) {
            if (fastNode.val != slowNode.val) {

                slowNode.next = fastNode;

                slowNode = slowNode.next;


            }
            fastNode = fastNode.next;
        }
        slowNode.next = null;
        return head;

//        if (head == null) {
//            return null;
//        }
//        ListNode slow = head, fast = head;
//        while (fast != null) {
//            if (fast.val != slow.val) {
//                // nums[slow] = nums[fast];
//                slow.next = fast;
//                // slow++;
//                slow = slow.next;
//            }
//            // fast++
//            fast = fast.next;
//        }
//        // 断开与后面重复元素的连接
//        slow.next = null;
//        return head;

    }

    public static void main(String[] args) {

        ListNode node = new ListNode(1, new ListNode(1, new ListNode(2, null)));
        ListNode node1 = deleteDuplicates(node);
        System.out.println();
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


}
